package com.googlecode.jmicroerp.wicket.butoaie;

import com.googlecode.jmicroerp.model.Apometre;
import com.googlecode.jmicroerp.model.Bon;
import com.googlecode.jmicroerp.model.ProdusVandut;
import com.googlecode.jmicroerp.model.Recipient;
import com.googlecode.jmicroerp.model.Stoc;
import com.googlecode.jmicroerp.service.ApometreService;
import com.googlecode.jmicroerp.service.GenericService;
import com.googlecode.jmicroerp.wicket.DefaultEditPage;
import java.math.BigDecimal;
import java.util.Date;
import org.apache.wicket.extensions.markup.html.form.DateTextField;
import org.apache.wicket.extensions.yui.calendar.DatePicker;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.SubmitLink;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.spring.injection.annot.SpringBean;

/**
 *
 * @author Ionel.David
 */
public class IndexAddPage extends DefaultEditPage {
    @SpringBean(name = "genericService")
    private GenericService<Apometre> apometreService;
    @SpringBean(name = "genericService")
    private GenericService<Recipient> recipientService;
    @SpringBean(name = "genericService")
    private GenericService<Stoc> produsService;
    @SpringBean(name = "genericService")
    private GenericService<ProdusVandut> produsVandutService;
    @SpringBean(name = "genericService")
    private GenericService<Bon> bonService;
    @SpringBean(name = "apometreService")
    private ApometreService apometruService;
    
    private Apometre apometre;
        
    public IndexAddPage(final Long recipientId) {
        final Recipient recipient = recipientService.getById(Recipient.class, recipientId);
        recipient.getStoc().getProdus().getPret();
        recipient.getTipRecipient().getCapacitate();
        apometre = new Apometre();
        apometre.setDataCitirii(new Date());
        apometre.setRecipient(recipient);
        
        Form<Apometre> form = new Form<Apometre>("form");
        form.setModel(new CompoundPropertyModel<Apometre>(apometre));
        
        form.add(new Label("mag", recipient.getMagazin().getDenumire()));
        form.add(new Label("recip", recipient.getDenumire()));
        
        DateTextField dtfDataCitirii = new DateTextField("dataCitirii", "dd.MM.yyyy");
        dtfDataCitirii.add(new DatePicker());
        dtfDataCitirii.setRequired(true);
        form.add(dtfDataCitirii);
        
        TextField<BigDecimal> tfCantitate = new TextField<BigDecimal>("valoare");
        tfCantitate.setRequired(true);
        form.add(new FeedbackPanel("feedback"));
        form.add(tfCantitate);
        
        SubmitLink salvare = new SubmitLink("submit") {

            @Override
            public void onSubmit() {
                BigDecimal lastIndex = apometruService.lastIndexByRecipient(recipientId);
                BigDecimal capacitateRecipient = recipient.getTipRecipient().getCapacitate();
                if (apometre.getValoare().compareTo(lastIndex.add(capacitateRecipient)) > 0) {
                    error(IndexAddPage.this.getString("index.prea.mare"));
                    return;
                }
                if (apometre.getId() == null) {
                    apometreService.insert(apometre);
                } else {
                    apometreService.update(apometre);
                }
                setResponsePage(ButoaiePage.class);
            }
            
        };
        form.add(salvare);
        
        form.add(new Link("renuntare") {

            @Override
            public void onClick() {
                setResponsePage(ButoaiePage.class);
            }
            
        });

        add(form);
    }
    
//    private BigDecimal calculIndex(Long recipientId) {
//        return apometre.getValoare().subtract(apometruService.lastIndexByRecipient(recipientId));
//    }
}